//
//  BDShopCouponsTableViewCell.m
//  BadBoy
//
//  Created by wen on 2017/7/4.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDShopCouponsTableViewCell.h"
#import "BDShopCouponsModel.h"

@interface BDShopCouponsTableViewCell ()

@property (nonatomic, strong) UILabel *currentPriceLabel;
@property (nonatomic, strong) UILabel *oldPriceLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *soldLabel;
@property (nonatomic, strong) UIButton *deletedButton;
@property (nonatomic, strong) UIButton *editButton;
@property (nonatomic, copy) void(^editButtonBlock)();
@property (nonatomic, copy) void(^deletedButtonBlock)();
@property (nonatomic, copy) void(^qianggouButtonBlock)();
@property (nonatomic, strong) UIView *bottomLine;
@property (nonatomic, strong) UIButton *qianggouButton;

@end

@implementation BDShopCouponsTableViewCell

- (void)configeWithModel:(id)model editButtonBlock:(void(^)())editButtonBlock deletedButtonBlock:(void(^)())deletedButtonBlock qianggouButtonBlock:(void(^)())qianggouButtonBlock{
    BDShopCouponsModel *couponsModel1 = model;
    self.textLabel.text = couponsModel1.title;
    
    NSString *soldString = [NSString stringWithFormat:@"¥%@", [NSDecimalNumber decimalNumberWithString:couponsModel1.discount_price]];
    NSMutableAttributedString *arrString = [[NSMutableAttributedString alloc] initWithString:soldString];
    NSRange range = [soldString rangeOfString:@"¥"];
    [arrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:range];
    
    self.currentPriceLabel.attributedText = arrString;
    
    
    NSString *priceString = [NSString stringWithFormat:ASLocalizedString(@"原价：¥%@"), [NSDecimalNumber decimalNumberWithString:couponsModel1.price]];
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:priceString];
    
    [attribtStr setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSBaselineOffsetAttributeName : @(NSUnderlineStyleSingle)} range:NSMakeRange(0,priceString.length)];

    // 赋值
    self.oldPriceLabel.attributedText = attribtStr;
    
    self.timeLabel.text = [NSString stringWithFormat:ASLocalizedString(@"有效期：%@ 至 %@"), couponsModel1.startTimeString, couponsModel1.endTimeString];
    
    self.soldLabel.text = [NSString stringWithFormat:ASLocalizedString(@"已售%@"),couponsModel1.statistics[@"sales"]];
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:couponsModel1.cover] placeholderImage:[UIImage imageNamed:@"shop_moren.jpg"]];
    
    self.editButton.hidden = YES;
    self.deletedButton.hidden = YES;
    self.qianggouButton.hidden= YES;
    
    if (editButtonBlock) {
        self.editButton.hidden = NO;
        self.editButtonBlock = editButtonBlock;
    }
    
    if (deletedButtonBlock) {
        self.deletedButton.hidden = NO;
        self.deletedButtonBlock = deletedButtonBlock;
    }
    
    if (qianggouButtonBlock) {
        self.qianggouButton.hidden = NO;
        self.qianggouButtonBlock = qianggouButtonBlock;
    }
    
//    if (needButton == YES) {
//        
//    }else{
//        self.editButton.hidden = YES;
//        self.deletedButton.hidden = YES;
//    }
//    self.editButtonBlock = editButtonBlock;
//    self.deletedButtonBlock = deletedButtonBlock;
}



- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.imageView.size = CGSizeMake(75, 65);
    self.imageView.left = 10;
    self.imageView.centerY = (self.height - 35) / 2.0;
    
    self.textLabel.font = [UIFont systemFontOfSize:15];
    self.textLabel.textColor = [UIColor colorWithHexString:@"#101010"];
    [self.textLabel sizeToFit];
    self.textLabel.left = self.imageView.right + 10;
    self.textLabel.top = self.imageView.top + 3;
    
    
    self.currentPriceLabel.textColor = [UIColor colorWithHexString:@"#ff4f00"];
    [self.currentPriceLabel sizeToFit];
    self.currentPriceLabel.left = self.textLabel.left;
    self.currentPriceLabel.bottom = self.imageView.bottom - 3;
    
    self.oldPriceLabel.font = [UIFont systemFontOfSize:10];
    self.oldPriceLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    [self.oldPriceLabel sizeToFit];
    self.oldPriceLabel.left = self.currentPriceLabel.right + 15;
    self.oldPriceLabel.bottom = self.currentPriceLabel.bottom;
    
    self.deletedButton.size = CGSizeMake(16, 16);
    self.deletedButton.right = self.width - 10;
    self.deletedButton.centerY = self.textLabel.centerY;
    [self.deletedButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
    
    self.editButton.size = CGSizeMake(16, 16);
    self.editButton.right = self.deletedButton.left - 20;
    self.editButton.centerY = self.textLabel.centerY;
    [self.editButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
    
    self.bottomLine.bottom = self.height - 35;
    
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    self.timeLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    [self.timeLabel sizeToFit];
    self.timeLabel.left = 10;
    self.timeLabel.centerY = self.height - 17.5;
    
    self.soldLabel.font = [UIFont systemFontOfSize:10];
    self.soldLabel.textColor = [UIColor colorWithHexString:@"999999"];
    [self.soldLabel sizeToFit];
    self.soldLabel.right = self.width - 15;
    self.soldLabel.centerY = self.height - 17.5;
    
    self.qianggouButton.centerY = self.imageView.centerY;
    self.qianggouButton.right = self.width - 15;
}

- (UIButton *)qianggouButton{
    if (!_qianggouButton) {
        self.qianggouButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_qianggouButton setTitle:ASLocalizedString(@"抢购")forState:UIControlStateNormal];
        [_qianggouButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _qianggouButton.titleLabel.font = [UIFont systemFontOfSize:13];
        _qianggouButton.backgroundColor = [UIColor colorWithHexString:@"4d7aad"];
        _qianggouButton.size = CGSizeMake(65, 30);
        _qianggouButton.layer.masksToBounds = YES;
        _qianggouButton.layer.cornerRadius = 3;
        [_qianggouButton setEnlargeEdgeWithTop:10 right:5 bottom:10 left:5];
        [[_qianggouButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (self.qianggouButtonBlock) {
                self.qianggouButtonBlock();
            }
        }];
        [self.contentView addSubview:_qianggouButton];
    }
    return _qianggouButton;
}

- (UIView *)bottomLine{
    if (!_bottomLine) {
        self.bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 0.5)];
        _bottomLine.backgroundColor = kBDColorGray;
        [self.contentView addSubview:_bottomLine];
    }
    return _bottomLine;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        self.timeLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_timeLabel];
    }
    return _timeLabel;
}

- (UILabel *)currentPriceLabel{
    if (!_currentPriceLabel) {
        self.currentPriceLabel = [[UILabel alloc] init];
        _currentPriceLabel.font = [UIFont systemFontOfSize:18];
        [self.contentView addSubview:_currentPriceLabel];
    }
    return _currentPriceLabel;
}

- (UILabel *)oldPriceLabel{
    if (!_oldPriceLabel) {
        self.oldPriceLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_oldPriceLabel];
    }
    return _oldPriceLabel;
}

-(UILabel *)soldLabel{
    if (!_soldLabel) {
        self.soldLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_soldLabel];
    }
    return _soldLabel;
}

- (UIButton *)deletedButton{
    if (!_deletedButton) {
        self.deletedButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deletedButton setImage:[UIImage imageNamed:@"shop_deleted"] forState:UIControlStateNormal];
        [self.contentView addSubview:_deletedButton];
        [[_deletedButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (self.deletedButtonBlock) {
                self.deletedButtonBlock();
            }
        }];
    }
    return _deletedButton;
}

- (UIButton *)editButton{
    if (!_editButton) {
        self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_editButton setImage:[UIImage imageNamed:@"shop_edit"] forState:UIControlStateNormal];
        [self.contentView addSubview:_editButton];
        [[_editButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (self.editButtonBlock) {
                self.editButtonBlock();
            }
        }];
    }
    return _editButton;
}
@end
